﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace UI.View.Feiyong.Part
{
    /// <summary>
    /// Pay.xaml 的交互逻辑
    /// </summary>
    public partial class Pay : UserControl
    {
        ViewModel.CostViewModel vm;
        SqlSugar.SqlSugarClient DB = new Model.DataContext().DB;
        public Pay()
        {
            InitializeComponent();

            vm = DataContext as  ViewModel.CostViewModel;
        }

        private void Btn_Save_Click(object sender, RoutedEventArgs e)
        {

            try
            {
                //保存费用

                var cost = new Model.F_CostRoom()
                {
                    WaterId = vm.CurrWater.Id,
                    RoomId = vm.CurrWater.RoomId,
                    RoomNo = vm.CurrWater.RoomNo,
                    OwnerName=vm.CurrWater.Owner,
                    CostType = vm.MeterType,
                    Amount = vm.PayAmount,
                    CreateTime = DateTime.Now,
                    UserId = Comm.TempData.LoginUser.Id,
                    UserName = Comm.TempData.LoginUser.Name,
                    PayType = cmb_FeeType.Text,
                };


                DB.Insertable<Model.F_CostRoom>(cost).ExecuteCommand();
                vm.CurrWater.IsCharged = true;
                DB.Updateable<Model.F_Water>(vm.CurrWater).ExecuteCommand();

                vm.ListWater =  DB.Queryable<Model.F_Water>().Where(w => w.MeterType == vm.MeterType).ToList();
                vm.Dialog.Close();
            }
            catch (Exception ex)
            {
                HandyControl.Controls.Growl.FatalGlobal(ex.Message);
                 
            }
           
        }

        private void Btn_Cancle_Click(object sender, RoutedEventArgs e)
        {
            vm.Dialog.Close();
        }

        private void ComboBox_DropDownOpened(object sender, EventArgs e)
        {
            vm.ListFeeTypes = DB.Queryable<Model.FeeType>().ToList();
        }
    }
}
